<template>
  <div class="container">
    <input type="text" class="todo-input" v-model="newTodo" @keyup.enter="addTodo">
    <div class="todo-list">
      <TodoItem :items="todos" @remove="handleRemove" @changeState="handleChangeState"></TodoItem>
    </div>
  </div>
</template>

<script>
import TodoItem from "@/components/TodoItem";

export default {
  name: "TodoList",
  components: {TodoItem},
  data() {
    return {
      todos: [],
      newTodo: '',
    }
  },
  methods: {
    addTodo() {
      this.todos.push({
        id: this.createId(),
        content: this.newTodo,
        state: 0,
      })
    },
    handleRemove(id) {
      this.todos = this.todos.filter(item => item.id != id)
    },
    handleChangeState(id) {

      this.todos = this.todos.map(item=>{
        if(item.id===id){
          item.state = !item.state
        }
        return item
      })
    },

    createId() {
      return Date.now();
    },
  },
}
</script>

<style scoped>
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.todo-input {
  width: 200px;
}

.todo-list {
  width: 200px;
}
</style>